/*
 * @Description: 系统配置
 * @Author: Zzt
 * @Date: 2022-11-11 14:52:39
 * @LastEditTime: 2023-01-30 14:04:02
 */
import VisualizationMain from '@/components/DataVisualization/VisualizationMain';
import Nav from '@/components/Nav';
import { Tabs } from 'antd';
import { CheckboxValueType } from 'antd/es/checkbox/Group';
import classNames from 'classnames/bind';
import { useEffect, useState } from 'react';
import styles from '../styles/userPermissions.module.scss';
import { useUserPermissionsContext } from '../contexts/UserPermissionsContext';
const { TabPane } = Tabs;
const cx = classNames.bind(styles);

const according = [
  { fondName: '按时间', id: 0 },
  { fondName: '按配置类型', id: 1 },
  { fondName: '按操作人', id: 2 }
];
const segmentedList = [
  { label: '增加', value: 'addCount' },
  { label: '删除', value: 'delCount' },
  { label: '修改', value: 'updateCount' },
  { label: '查看', value: 'selectCount' }
];
const table = [
  { title: '新建', dataIndex: 'addCount', key: 'addCount' },
  { title: '删除', dataIndex: 'delCount', key: 'delCount' },
  { title: '修改', dataIndex: 'updateCount', key: 'updateCount' },
  { title: '查看', dataIndex: 'selectCount', key: 'selectCount' }
];
const UserPermissionsWarp = () => {
  const { setChartData, setTableData, dataList, chartList } = useUserPermissionsContext();
  const [fondValue, setFondValue] = useState({});
  const [limit, setLimit] = useState<any>({});
  const [segmented, setSegmented] = useState('addCount');
  // 选中的按照类型
  const [type, setType] = useState(0);
  const [options, setOptions] = useState([
    {
      title: '操作人',
      name: 'optId',
      checkList: [
        { value: 1, label: '操作人1' },
        { value: 2, label: '操作人2' },
        { value: 3, label: '操作人3' },
        { value: 4, label: '操作人4' }
      ]
    },
    {
      title: '配置类型',
      name: 'configType',
      checkList: [
        { value: 1, label: '用户管理' },
        { value: 2, label: '角色管理' },
        { value: 3, label: '部门管理' },
        { value: 4, label: '档案管理权限' },
        { value: 5, label: '利用者知悉范围' }
      ]
    }
  ]);
  const [visibleData, setVisibleData] = useState([]);
  const [condition, setCondition] = useState(<></>);
  const [tableList, setTableList] = useState(table);
  const [xData, setXData] = useState([]);
  const [yData, setYData] = useState([]);

  const [listQuery, setListQuery] = useState({
    pageNo: 1,
    pageSize: 10,
    param: {}
  });
  /**
   * @description: 切换全宗
   * @param {number} val 选中的全宗
   */
  const handleAccordingChange = (val: any) => {
    console.log(val);
    setType(parseInt(val, 10));
  };
  const onChange = (checkedValues: CheckboxValueType[]) => {
    console.log('checked = ', checkedValues);
  };
  const onChangeNav = (value: any) => {
    setFondValue(value);
    console.log('value', value);
  };
  useEffect(() => {
    setChartData({ ...limit, type, ...fondValue });
  }, [limit, type, fondValue]);
  useEffect(() => {
    const param = { ...limit, type, ...fondValue, menuType: 2 };
    setTableData({ ...listQuery, param });
  }, [limit, type, fondValue, listQuery]);
  useEffect(() => {
    setVisibleData(dataList);
    setYData(chartList.map((item: any) => item.name));
    setXData(chartList.map((item: any) => item[segmented]));
  }, [dataList, chartList, segmented]);

  const ChangNo = (num: any) => {
    setListQuery((state) => {
      return { ...state, pageNo: num };
    });
  };

  useEffect(() => {
    const removed = table.slice(0);
    if (type === 0) {
      removed.splice(
        0,
        0,
        { title: '时间', dataIndex: 'time', key: 'time' },
        { title: '配置类型', dataIndex: 'configurationType', key: 'configurationType' },
        { title: '操作人员', dataIndex: 'operators', key: 'operators' }
      );
      setTableList(removed);
      setCondition(<></>);
    }
    if (type === 1) {
      removed.splice(
        0,
        0,
        { title: '配置类型', dataIndex: 'configurationType', key: 'configurationType' },
        { title: '时间', dataIndex: 'time', key: 'time' },
        { title: '操作人员', dataIndex: 'operators', key: 'operators' }
      );
      setTableList(removed);
      setCondition(<></>);
    }
    if (type === 2) {
      removed.splice(
        0,
        0,
        { title: '操作人员', dataIndex: 'operators', key: 'operators' },
        { title: '配置类型', dataIndex: 'configurationType', key: 'configurationType' },
        { title: '时间', dataIndex: 'time', key: 'time' }
      );
      setTableList(removed);
      setCondition(<></>);
    }
  }, [type]);
  /* 改变触发 */
  const onChangeYear = (value: any) => {
    setLimit(value.chioce);
    setSegmented(value.segmentValue);
  };
  return (
    <>
      <Tabs tabPosition="left" className={cx('tab')} onTabClick={handleAccordingChange}>
        {according.map((item) => (
          <TabPane tab={<>{item.fondName}</>} key={item.id}>
            <VisualizationMain
              getList={setTableData}
              type={item.fondName}
              condition={condition}
              segmented={segmentedList}
              tableList={tableList}
              choiceList={options}
              data={{ visibleData, xData, yData }}
              onChange={onChangeYear}
              ChangNo={ChangNo}
            />
          </TabPane>
        ))}
      </Tabs>
    </>
  );
};
export default UserPermissionsWarp;
